document.getElementById("begin_time") && bmCommonFun.initDateTimePicker("begin_time");
document.getElementById("end_time") && bmCommonFun.initDateTimePicker("end_time");

search_data && bmCommonFun.searchList({
    url: search_data.url,
    data: search_data.data
});

var vm_add_admin = avalon.define({
    $id: "vm-add_admin",
    is_disabled: false,
    form_data: {
	  	name: '',
        is_effect: 1,
        bm_role_id: '',
        password: '',
        id: null
    },
    role_list: [],
    admin_info: '',
    form_type: 'admin_add',
    check(){
    	let self = this;
	 	if ($checkAction.checkEmpty(self.form_data.name)) {
            layer.msg('请输入管理员账号');
            return false;
        }
        if($checkAction.checkEmpty(self.form_data.password)){
        	layer.msg('请输入管理员密码');
            return false;
        }
        if($checkAction.checkEmpty(self.form_data.bm_role_id)){
        	layer.msg('请选中管理员组');
            return false;
        }
        else{
        	return true;
        }
    },
    submit: function(){
        // 提交更新
        let self = this;
    	if(self.check()){
    		let loading = layer.load();
	        $handleAjax.handle({
	            url: APP_ROOT + "/mapi/index.php?ctl=user_two&act="+self.form_type,
	            isTip: false,
	            data: self.form_data
	        }).done(function(result){
	        	layer.close(loading);
	            if(result.status == 1){
	            	layer.msg(result.error || '操作成功',{
	            		time: 1000
	            	});
	            	setTimeout(function(){
	            		location.reload();
	            	}, 1000);
	            }
	           	else{
	           		layer.msg(result.error || '操作失败');	
	           	}

	        }).fail(function(err){
	            console.log(err);
	        });
    	}
    }
});

var vm_edit = avalon.define({
    $id: "vm-edit",
    hasCheckAll: false,
    hasCheck: false,
    hasRoleList: false,
    htmlEditAdmin: `<div style="padding:15px 20px;" ms-important="vm-add_admin" id="add_admin" class="ms-controller">
                                <form class="form-horizontal">
                                    <div class="form-group">
                                        <label for="name" class="col-sm-3 control-label">管理员名称:</label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" id="name" ms-duplex="@form_data.name" placeholder="请输入管理员帐号">
                                        </div>
                                        </div>
                                    <div class="form-group">
                                        <label for="password" class="col-sm-3 control-label">管理员密码:</label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" id="password" ms-duplex="@form_data.password" placeholder="请输入管理员密码">
                                        </div>
                                        </div>
                                    <div class="form-group">
                                        <label for="password" class="col-sm-3 control-label">状态:</label>
                                        <div class="col-sm-8">
                                            <label class="radio-inline">
                                                <input type="radio" ms-duplex="@form_data.is_effect" id="is_effect" checked="checked" value="1"> 有效
                                            </label>
                                            <label class="radio-inline">
                                                <input type="radio" ms-duplex="@form_data.is_effect" id="is_effect" value="0"> 无效
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="new_pwd" class="col-sm-3 control-label">&nbsp;&nbsp;&nbsp;管理员组:</label>
                                        <div class="col-sm-8">
                                            <select class="form-control" :duplex="@form_data.bm_role_id">
                                                <option ms-for="el in @role_list" :attr="{value: @el.id, selected: @el.id==@form_data.bm_role_id}">{{el.name}}</option>
                                            </select>
                                        </div>
                                    </div>
                                </form>
                                <a class="btn btn-primary mt-20" ms-click="@submit" style="margin-left:84px;">确认新增</a>
                            </div>`,
    getRoleList(type,id=null){
        let self = this;
        $handleAjax.handle({
            url: APP_ROOT + "/mapi/index.php?ctl=user_two&act="+type,
            type: "GET",
            data: {
                id: id
            },
            dataType: "json",
            isTip: false
        }).done(function(result){
            if(type == 'admin_add'){
                if(result.status == 1){
                    self.hasRoleList = true;
                    result.role_list.forEach(function(element) {
                        vm_add_admin.role_list.push({name: element.name, id: element.id});
                    }, this);
                    vm_add_admin.form_data = {
                        name: '',
                        is_effect: 1,
                        bm_role_id: vm_add_admin.role_list[0].id,
                        password: '',
                        id: null
                    }
                    vm_add_admin.form_type = "admin_insert";
                }
                else{
                    self.hasRoleList = false;
                    layer.msg(result.error || '操作失败');
                }
            }
            else{
                if(result.status == 0){
                    self.hasRoleList = false;
                    layer.msg(result.error || '操作失败');
                }
                else{
                    for (var i in result.admin_info[0]) {
                        if(vm_add_admin.form_data[i] !== undefined) {
                            vm_add_admin.form_data[i] = result.admin_info[0][i];
                        }
                    }
                    result.role_list.forEach(function(element) {
                        vm_add_admin.role_list.push({name: element.name, id: element.id});
                    }, this);
                    self.hasRoleList = true;
                    vm_add_admin.form_type = "admin_update";
                    vm_add_admin.form_data.id = id;
                    vm_add_admin.form_data.bm_role_id = result.admin_info[0].bm_role_id;
                }
            }

        }).fail(function(err){
            layer.msg(err || '操作失败');
            return false;
        });
    },
    addAdmin(){
        // 新增管理员
        let self = this;
        self.getRoleList("admin_add");
        setTimeout(() =>{
            if(self.hasRoleList){
                layer.open({
                    type: 1,
                    title: '新增管理员',
                    skin: 'layui-layer-rim',
                    area: ['500px', '350px'],
                    content: self.htmlEditAdmin,
                    success: function(layero, index){
                        avalon.scan(document.getElementById('add_admin'));
                        self.hasRoleList = false;
                    },
                    cancel: function(index, layero){ 
                        vm_add_admin.form_data = {
                            name: '',
                            is_effect: 1,
                            bm_role_id: '',
                            password: '',
                            id: null
                        }
                        vm_add_admin.role_list = [];
                    }
                });
            }
        },500);
       
    },
    editAdmin(id){
        // 编辑管理员
        let self = this;
        self.getRoleList("admin_edit",id);
        setTimeout(()=>{
            if(self.hasRoleList){
                layer.open({
                    type: 1,
                    title: '编辑管理员',
                    skin: 'layui-layer-rim',
                    area: ['500px', '350px'],
                    content: self.htmlEditAdmin,
                    success: function(layero, index){
                        avalon.scan(document.getElementById('add_admin'));
                        self.hasRoleList = false;
                    },
                    cancel: function(index, layero){ 
                        vm_add_admin.form_data = {
                            name: '',
                            is_effect: 1,
                            bm_role_id: '',
                            password: '',
                            id: null
                        }
                        vm_add_admin.role_list = [];
                    }
                });
            }
        },500);
    },
    delAdmin(){
        layer.confirm('确认删除已选中的项？', {
        btn: ['取消','删除'] //按钮
        }, function(){
            // 已取消操作
        }, () => {
            // 删除操作
        });
    },
    checkAll(e){
        $(e.target).is(':checked') && $(".single_check").attr("checked", true) || $(".single_check").removeAttr("checked");
    }
});